package com.bootlever.framework.core.properties.basic;

import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class FastJsonParser {
    private static final Logger logger = LoggerFactory.getLogger(FastJsonParser.class);

    private static final String DEFAULT_NAME = "fastjson";
    private static final boolean DEFAULT_WRITE_NULL_LIST_AS_EMPTY = true;
    private static final boolean DEFAULT_WRITE_NULL_STRING_AS_EMPTY = true;
    private static final boolean DEFAULT_DISABLE_CIRCULAR_REFERENCE_DETECT = true;
    private static final boolean DEFAULT_WRITE_NULL_BOOLEAN_AS_FALSE = false;
    private static final boolean DEFAULT_WRITE_MAP_NULL_VALUE = true;

    private List<SerializerFeature> features = new ArrayList<SerializerFeature>();
    /**
     * 名称
     */
    private String name = DEFAULT_NAME;
    /**
     * 空list输出[]而不是null
     */
    private boolean writeNullListAsEmpty = DEFAULT_WRITE_NULL_LIST_AS_EMPTY;

    /**
     * null转空字符串
     */
    private boolean writeNullStringAsEmpty = DEFAULT_WRITE_NULL_STRING_AS_EMPTY;

    /**
     * 是否禁止循环引用
     */
    private boolean isDisableCircularReferenceDetect = DEFAULT_DISABLE_CIRCULAR_REFERENCE_DETECT;

    /**
     * Boolean为null时转false
     */
    private boolean writeNullBooleanAsFalse = DEFAULT_WRITE_NULL_BOOLEAN_AS_FALSE;

    /**
     * 是否输出value为null的字段
     */
    private boolean writeMapNullValue = DEFAULT_WRITE_MAP_NULL_VALUE;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isWriteNullListAsEmpty() {
        return writeNullListAsEmpty;
    }

    public void setWriteNullListAsEmpty(boolean writeNullListAsEmpty) {
        this.writeNullListAsEmpty = writeNullListAsEmpty;
    }

    public boolean isWriteNullStringAsEmpty() {
        return writeNullStringAsEmpty;
    }

    public void setWriteNullStringAsEmpty(boolean writeNullStringAsEmpty) {
        this.writeNullStringAsEmpty = writeNullStringAsEmpty;
    }

    public boolean isDisableCircularReferenceDetect() {
        return isDisableCircularReferenceDetect;
    }

    public void setDisableCircularReferenceDetect(boolean disableCircularReferenceDetect) {
        isDisableCircularReferenceDetect = disableCircularReferenceDetect;
    }

    public boolean isWriteNullBooleanAsFalse() {
        return writeNullBooleanAsFalse;
    }

    public void setWriteNullBooleanAsFalse(boolean writeNullBooleanAsFalse) {
        this.writeNullBooleanAsFalse = writeNullBooleanAsFalse;
    }

    public boolean isWriteMapNullValue() {
        return writeMapNullValue;
    }

    public void setWriteMapNullValue(boolean writeMapNullValue) {
        this.writeMapNullValue = writeMapNullValue;
    }

    public List<SerializerFeature> getFeatures() {
        addFeatures();
        return features;
    }

    private void addFeatures(){
        if (writeNullListAsEmpty == true) {
            features.add(SerializerFeature.WriteNullListAsEmpty);
        }
        if (writeNullStringAsEmpty == true) {
            features.add(SerializerFeature.WriteNullStringAsEmpty);
        }
        if (isDisableCircularReferenceDetect == true) {
            features.add(SerializerFeature.DisableCircularReferenceDetect);
        }
        if (writeNullBooleanAsFalse == true) {
            features.add(SerializerFeature.WriteNullBooleanAsFalse);
        }
        if (writeMapNullValue == true) {
            features.add(SerializerFeature.WriteMapNullValue);
        }
    }
}
